#include "mem_proc.h"
#include "dbg_trace.h"


//添加页到空闲链表中
MY_VOID AddPageToFreeList(paddr_t pa, MY_U8 u8Order)
{
//	paddr_t pa = 0x12345678;
	MY_U8 bit;
	MY_U32 n;
	
	u8Order = VM_PHYS_TO_ORDER(pa);
	DBG_TRACE_INFO("--0-- u8Order = %d.", u8Order);
	n = VM_ORDER_TO_PAGES(u8Order);
	
	do
	{
		pa ^= VM_ORDER_TO_PHYS(u8Order);
		DBG_TRACE_INFO("--1-- pa = 0x%04x.", pa);

		u8Order++;
	
		pa &= ~(VM_ORDER_TO_PHYS(u8Order) - 1);
		DBG_TRACE_INFO("--2-- pa = 0x%04x.\n", pa);
	}
	while(u8Order < VM_LIST_ORDER_MAX - 1);
}


VOID VmPhysPagesFreeContiguous(paddr_t pa, MY_U16 nPages)
{
    MY_U32 order;
    MY_U32 count;
    MY_U32 n;

    while(1)
    {
        pa = VM_PAGE_TO_PHYS(pa);			//获取物理地址
        order = VM_PHYS_TO_ORDER(pa);		//物理地址 >> 12后，计算后导零得出order的值，max 8
        n = VM_ORDER_TO_PAGES(order);		//2 ^ order(max -- 8)
        if(n > nPages)
        {
            break;
        }
        AddPageToFreeList(pa, order);
        nPages -= n;
        pa += (n*0x1000);
    }

    for(count = 0; count < nPages; count += n)
    {
        order = LOS_HighBitGet(nPages);
        n = VM_ORDER_TO_PAGES(order);
        AddPageToFreeList(pa, order);
        pa += (n*0x1000);
    }
}


